//
//  TransferManagerCell.m
//  PigMarket
//
//  Created by mac on 2019/5/9.
//  Copyright © 2019 felix.pigmarket.oc. All rights reserved.
//

#import "TransferManagerCell.h"

@interface TransferManagerCell ()
/**
 bg
 */
@property (nonatomic , strong) UIView *bgView;
/**
 title
 */
@property (nonatomic , strong) UILabel *titleLab;
/**
 money
 */
@property (nonatomic , strong) UILabel *moneyLab;
/**
 line
 */
@property (nonatomic , strong) UIView *line;
/**
 receiver
 */
@property (nonatomic , strong) UILabel *receiverLab;
/**
 receiverDetail
 */
@property (nonatomic , strong) UILabel *receiverDetailLab;
/**
 variety
 */
@property (nonatomic , strong) UILabel *varietyLab;
/**
 varietyDetailLab
 */
@property (nonatomic , strong) UILabel *varietyDetailLab;
/**
 pigNum
 */
@property (nonatomic , strong) UILabel *pigNumLab;
/**
 pigNumDetailLab
 */
@property (nonatomic , strong) UILabel *pigNumDetailLab;
/**
 transferTime
 */
@property (nonatomic , strong) UILabel *transferTimeLab;
/**
 transferDetailTime
 */
@property (nonatomic , strong) UILabel *transferDetailTimeLab;
/**
 statusBtn
 */
@property (nonatomic , strong) UIButton *statusBtn;
@end
@implementation TransferManagerCell

+ (instancetype)cellWithTableView:(UITableView *)tableView {
    static NSString *identifierID = @"identifierID";
    TransferManagerCell *cell = [tableView dequeueReusableCellWithIdentifier:identifierID];
    if (cell == nil) {
        cell = [[TransferManagerCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifierID];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.backgroundColor = [UIColor clearColor];
    return cell;
}
#pragma mark - init method
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self initView];
        [self initLayout];
    }
    return self;
}

- (void)initView {
    // 添加子控件
    [self.contentView addSubview:self.bgView];
    [self.bgView addSubview:self.titleLab];
    [self.bgView addSubview:self.moneyLab];
    [self.bgView addSubview:self.line];
    [self.bgView addSubview:self.receiverLab];
    [self.bgView addSubview:self.receiverDetailLab];
    [self.bgView addSubview:self.statusBtn];
    [self.bgView addSubview:self.varietyLab];
    [self.bgView addSubview:self.varietyDetailLab];
    [self.bgView addSubview:self.pigNumLab];
    [self.bgView addSubview:self.pigNumDetailLab];
    [self.bgView addSubview:self.transferTimeLab];
    [self.bgView addSubview:self.transferDetailTimeLab];
}

- (void)initLayout {
    // 布局子控件
    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.left.mas_equalTo(15);
        make.right.mas_equalTo(-15);
        make.height.mas_equalTo(200);
    }];
    [self.titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(13);
        make.left.mas_equalTo(15);
        make.height.mas_equalTo(21);
    }];
    [self.moneyLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.titleLab.mas_centerY);
        make.right.mas_equalTo(-15);
        make.height.mas_equalTo(22);
    }];
    [self.line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.top.mas_equalTo(self.titleLab.mas_bottom).offset(11);
        make.height.mas_equalTo(1);
    }];
    [self.receiverLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.top.mas_equalTo(self.line.mas_bottom).offset(20);
        make.height.mas_equalTo(20);
    }];
    [self.receiverDetailLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(104);
        make.centerY.mas_equalTo(self.receiverLab.mas_centerY);
        make.height.mas_equalTo(20);
    }];
    [self.statusBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.receiverLab.mas_centerY);
        make.right.mas_equalTo(-15);
        make.size.mas_equalTo(CGSizeMake(82, 30));
    }];
    [self.varietyLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.top.mas_equalTo(self.receiverDetailLab.mas_bottom).offset(12);
        make.height.mas_equalTo(20);
    }];
    [self.varietyDetailLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(104);
        make.centerY.mas_equalTo(self.varietyLab.mas_centerY);
        make.height.mas_equalTo(20);
    }];
    [self.pigNumLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.top.mas_equalTo(self.varietyDetailLab.mas_bottom).offset(12);
        make.height.mas_equalTo(20);
    }];
    [self.pigNumDetailLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(104);
        make.centerY.mas_equalTo(self.pigNumLab.mas_centerY);
        make.height.mas_equalTo(20);
    }];
    [self.transferTimeLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.top.mas_equalTo(self.pigNumDetailLab.mas_bottom).offset(12);
        make.height.mas_equalTo(20);
    }];
    [self.transferDetailTimeLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(104);
        make.centerY.mas_equalTo(self.transferTimeLab.mas_centerY);
        make.height.mas_equalTo(20);
    }];
}
- (void)configCellWithModel:(TransferManager *)model transferManagerType:(TransferManagerType)transferManagerType {
    self.moneyLab.text = [NSString stringWithFormat:@"￥%@",model.money];
    self.receiverDetailLab.text = model.phone;
    self.varietyDetailLab.text = model.varietiesName;
    self.pigNumDetailLab.text = model.pigNum;
    self.transferDetailTimeLab.text = model.countdown;
    if (transferManagerType == TransferManagerTypePig) {//被转让的猪
        [self.statusBtn setTitle:@"撤销" forState:UIControlStateNormal];
        self.statusBtn.layer.borderWidth = 1;
        self.statusBtn.layer.borderColor = [UIColor colorWithHexString:@"#D8D8D8"].CGColor;
        self.statusBtn.layer.cornerRadius =  15;
        

    } else if (transferManagerType == TransferManagerTypeRecord) {//转让记录
        self.titleLab.text = @"转让成功";
        self.receiverLab.textColor = [UIColor colorWithHexString:@"#888888"];
        self.receiverDetailLab.textColor = [UIColor colorWithHexString:@"#888888"];
        self.transferTimeLab.textColor = [UIColor colorWithHexString:@"#888888"];
        self.transferDetailTimeLab.textColor = [UIColor colorWithHexString:@"#888888"];

        if ([model.status intValue] == 3) {//拒绝
            self.moneyLab.textColor = [UIColor colorWithHexString:@"#888888"];
            [self.statusBtn setBackgroundImage:[UIImage imageNamed:@"adopted_transfermanager_refuse"] forState:UIControlStateNormal];
            [self.statusBtn mas_updateConstraints:^(MASConstraintMaker *make) {
                make.size.mas_equalTo(CGSizeMake(59, 59));
            }];
        }
    }
}
- (void)cancelTapped {
    if (self.cancelBlock) {
        self.cancelBlock();
    }
}
#pragma mark - getter and setter
- (UIView *)bgView {
    if (!_bgView) {
        _bgView = [[UIView alloc] init];
        _bgView.backgroundColor = [UIColor whiteColor];
        _bgView.layer.cornerRadius = 8;
        _bgView.layer.masksToBounds = YES;
    }
    return _bgView;
}
- (UILabel *)titleLab {
    if (!_titleLab) {
        _titleLab = [[UILabel alloc] init];
        _titleLab.textColor = [UIColor textBlackColor];
        _titleLab.font = [UIFont systemFontOfSize:15];
        _titleLab.text = @"转让金额";
    }
    return _titleLab;
}
- (UILabel *)moneyLab {
    if (!_moneyLab) {
        _moneyLab = [[UILabel alloc] init];
        _moneyLab.textColor = [UIColor colorWithHexString:@"#F25C62"];
        _moneyLab.font = [UIFont smallFont];
    }
    return _moneyLab;
}
- (UIView *)line {
    if (!_line) {
        _line = [[UIView alloc] init];
        _line.backgroundColor = [UIColor colorWithHexString:@"#000000" alpha:0.08];
    }
    return _line;
}
- (UILabel *)receiverLab {
    if (!_receiverLab) {
        _receiverLab = [[UILabel alloc] init];
        _receiverLab.textColor = [UIColor colorWithHexString:@"#333333"];
        _receiverLab.font = [UIFont middleFont];
        _receiverLab.text = @"接收人";
    }
    return _receiverLab;
}
- (UILabel *)receiverDetailLab {
    if (!_receiverDetailLab) {
        _receiverDetailLab = [[UILabel alloc] init];
        _receiverDetailLab.textColor = [UIColor colorWithHexString:@"#333333"];
        _receiverDetailLab.font = [UIFont middleFont];
    }
    return _receiverDetailLab;
}
- (UIButton *)statusBtn {
    if (!_statusBtn) {
        _statusBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_statusBtn setTitleColor:[UIColor colorWithHexString:@"#454545"] forState:UIControlStateNormal];
        _statusBtn.titleLabel.font = [UIFont systemFontOfSize:13];
        [_statusBtn addTarget:self action:@selector(cancelTapped) forControlEvents:UIControlEventTouchUpInside];
    }
    return _statusBtn;
}

- (UILabel *)varietyLab {
    if (!_varietyLab) {
        _varietyLab = [[UILabel alloc] init];
        _varietyLab.textColor = [UIColor colorWithHexString:@"#888888"];
        _varietyLab.font = [UIFont middleFont];
        _varietyLab.text = @"仔猪品种";
    }
    return _varietyLab;
}
- (UILabel *)varietyDetailLab {
    if (!_varietyDetailLab) {
        _varietyDetailLab = [[UILabel alloc] init];
        _varietyDetailLab.textColor = [UIColor colorWithHexString:@"#888888"];
        _varietyDetailLab.font = [UIFont middleFont];
    }
    return _varietyDetailLab;
}
- (UILabel *)pigNumLab {
    if (!_pigNumLab) {
        _pigNumLab = [[UILabel alloc] init];
        _pigNumLab.textColor = [UIColor colorWithHexString:@"#888888"];
        _pigNumLab.font = [UIFont middleFont];
        _pigNumLab.text = @"仔猪编号";
    }
    return _pigNumLab;
}
- (UILabel *)pigNumDetailLab {
    if (!_pigNumDetailLab) {
        _pigNumDetailLab = [[UILabel alloc] init];
        _pigNumDetailLab.textColor = [UIColor colorWithHexString:@"#888888"];
        _pigNumDetailLab.font = [UIFont middleFont];
    }
    return _pigNumDetailLab;
}
- (UILabel *)transferTimeLab {
    if (!_transferTimeLab) {
        _transferTimeLab = [[UILabel alloc] init];
        _transferTimeLab.textColor = [UIColor colorWithHexString:@"#333333"];
        _transferTimeLab.font = [UIFont middleFont];
        _transferTimeLab.text = @"转让中";
    }
    return _transferTimeLab;
}
- (UILabel *)transferDetailTimeLab {
    if (!_transferDetailTimeLab) {
        _transferDetailTimeLab = [[UILabel alloc] init];
        _transferDetailTimeLab.textColor = [UIColor colorWithHexString:@"#F25C62"];
        _transferDetailTimeLab.font = [UIFont middleFont];
    }
    return _transferDetailTimeLab;
}

@end
